package com.liuwei.test;

import junit.framework.TestCase;
import org.junit.Test;

/**
 * Created by liuwei on 2014/11/6.
 */


abstract class OperationFactory {
    public abstract void setOperator(double numberA, double numberB);

    public abstract double GetResult();

    public static OperationFactory factory(String type) {
        if (type.equals("+"))
            return new OperAdd();

        if (type.equals("-"))
            return new OperSub();

        if (type.equals("/"))
            return new OperDiv();

        if (type.equals("*"))
            return new OperMul();

        throw new RuntimeException("Bad shape creation: " + type);
    }
}

class OperAdd extends OperationFactory {
    private double numA = 0.0;
    private double numB = 0.0;

    OperAdd() {
    }

    public void setOperator(double numberA, double numberB) {
        this.numA = numberA;
        this.numB = numberB;
    }

    public double GetResult() {
        return (numA + numB);
    }
}

class OperSub extends OperationFactory {
    private double numA = 0.0;
    private double numB = 0.0;

    OperSub() {
    }

    public void setOperator(double numberA, double numberB) {
        this.numA = numberA;
        this.numB = numberB;
    }

    public double GetResult() {
        return (numA - numB);
    }
}

class OperMul extends OperationFactory {
    private double numA = 0.0;
    private double numB = 0.0;

    OperMul() {
    }

    public void setOperator(double numberA, double numberB) {
        this.numA = numberA;
        this.numB = numberB;
    }

    public double GetResult() {
        return (numA * numB);
    }
}

class OperDiv extends OperationFactory {
    private double numA = 0.0;
    private double numB = 0.0;

    OperDiv() {
    }

    public void setOperator(double numberA, double numberB) {
        this.numA = numberA;
        this.numB = numberB;
    }

    public double GetResult() {
        if (numB == 0.0)
            throw new IllegalArgumentException("divide by 0!");

        return (numA / numB);
    }
}

public class SimpleFactoryOper extends TestCase {
    @Test
    public void testAdd() {
        OperationFactory oper = OperationFactory.factory("+");
        oper.setOperator(10.0, 1.0);
        assertEquals("error in Add():", 11.0, oper.GetResult());
    }

    @Test
    public void testSub() {
        OperationFactory oper = OperationFactory.factory("-");
        oper.setOperator(10.0, 1.0);
        assertEquals("error in Sub():", 9.0, oper.GetResult());
    }

    @Test
    public void testDiv() {
        OperationFactory oper = OperationFactory.factory("/");
        oper.setOperator(10.0, 2.0);
        assertEquals("error in Div():", 5.0, oper.GetResult());
    }


    @Test(expected = IllegalArgumentException.class)
    public void testDivByZero() {
        OperationFactory oper = OperationFactory.factory("/");
        oper.setOperator(10.0, 0.0);
        System.out.println(oper.GetResult());
    }
    
    @Test
    public void testMul(){
        
        OperationFactory oper = OperationFactory.factory("*");
        oper.setOperator(2,0);

        assertEquals("error in Mul():",0,oper.GetResult()); //2*0
        
        oper.setOperator(2,4);
    	
    	assertEquals("error in Mul():",7,oper.GetResult()); //2*0
    }
    
    public static void main(String args[]) {
        junit.textui.TestRunner.run(SimpleFactoryOper.class);
    }

}
